<template>
  <div>

    <div v-model="searchData" style="margin: 10px 0">
      <el-input v-model="searchData.username" style="width: 200px" placeholder="请输入名称"
                suffix-icon="el-icon-search"></el-input>
      <el-input v-model="searchData.email" style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message"
                class="ml-5"></el-input>
      <el-input v-model="searchData.address" style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position"
                class="ml-5"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="rest">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="addHander">新增 <i class="el-icon-circle-plus-outline"></i>
      </el-button>
      <el-button type="danger" @click="delBatch">批量删除 <i class="el-icon-remove-outline"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="Id" width="40"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120"></el-table-column>
      <el-table-column prop="role" label="角色" width="120"></el-table-column>
      <el-table-column prop="nickname" label="昵称"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="look(scope.row.id)">查看课程 <i class="el-icon-search"></i></el-button>
          <el-button type="success" @click="handleEdit(scope.row.id)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增教师信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role" placeholder="请选择" style="width: 100%">
            <el-option
                v-for="item in options"
                :key="item.flag"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改教师信息" :visible.sync="updateFormVisible" width="30%">
      <el-form :model="info" label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="info.username" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="info.role" placeholder="请选择" style="width: 100%">
            <el-option
                v-for="item in options"
                :key="item.flag"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="info.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="info.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="info.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="info.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="课程信息" :visible.sync="lookVisible" width="50%">
      <el-table :data="courses" border stripe>
        <el-table-column prop="name" label="课程名称"></el-table-column>
        <el-table-column prop="score" label="学分"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Teacher",
  data() {
    return {
      multipleSelection: [],
      searchData: {
        username: '',
        email: '',
        address: ''
      },
      courses: [],
      form: {},
      info: {},
      tableData: [],
      headerBg: 'headerBg',
      current: 0,
      total: 0,
      pageSize: 5,
      dialogFormVisible: false,
      updateFormVisible: false,
      lookVisible: false,
      options: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    handleSelectionChange(val) {
      console.log("我想看到的")
      console.log(val)
      this.multipleSelection = val
      console.log(this.multipleSelection.map(v => v.id))
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  //[1,2,3]
      this.$confirm('此操作将删除这些用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/user/delete/batch/" + ids).then(resp => {
          if (resp.data) {
            this.load()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    del(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/user/delete/" + id).then(resp => {
          if (resp.data) {
            this.load()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addHander() {
      this.dialogFormVisible = true
      this.form = {}

      //获得角色下拉的选项
      this.request.get("/role").then(res => {
        console.log(res)
        this.options = res.data
      })
    },

    rest() { //重置搜索栏
      this.searchData.username = ''
      this.searchData.email = ''
      this.searchData.address = ''
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log("每页条数: " + pageSize)
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(current) {
      console.log("当前页码: " + current)
      this.current = current;
      this.load()
    },
    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    load() {
      this.request.get("/user/teacher/page", {
            params: {
              current: this.current,
              pageSize: this.pageSize,
              username: this.searchData.username,
              email: this.searchData.email,
              address: this.searchData.address
            }
          }
      ).then(resp => {
        console.log(resp)
        this.tableData = resp.data.records
        this.total = resp.data.total
      })
    },
    save() {//新增用户
      this.request.post("/user/save", this.form).then(resp => {
        console.log(resp)
        if (resp.data) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
          this.load()
        }
      })
    },
    //编辑
    handleEdit(id) {
      console.log(id)
      this.request.get("/user/" + id).then(resp => {
        console.log("根据id查到的数据")
        console.log(resp)
        this.info = resp.data
      })
      //获得角色下拉的选项
      this.request.get("/role").then(res => {
        console.log(res)
        this.options = res.data
      })
      this.updateFormVisible = true

    },
    //更新数据
    update() {
      this.request.put("/user/update/", this.info).then(resp => {
        console.log(111111)
        console.log(this.info)
        console.log(111111)
        console.log(resp)
        if (resp.data) {
          this.$message.success("更新成功")
          this.updateFormVisible = false
          this.load()
        } else {
          this.$message.error("更新失败")
        }
      })
    },
    //导出
    exp() {
      window.open("/user/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
    handleExcelImportError() {
      this.$message.success("导入失败")
    },
    look(id) {
      this.lookVisible = true
      this.request.get("/course/byTeacher/"+id).then(res=>{
        console.log(111112222)
        console.log(res)
        if (res.data){
          this.courses = res.data
        }
      })
    }
  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>